home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / parted / partition.py < prev   
Encoding:
Python Source  |  2010-06-29  |  9.2 KB  |  250 lines

  1. #
  2. # partition.py
  3. # Python bindings for libparted (built on top of the _ped Python module).
  4. #
  5. # Copyright (C) 2009 Red Hat, Inc.
  6. #
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13. # Public License for more details.  You should have received a copy of the
  14. # GNU General Public License along with this program; if not, write to the
  15. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
  17. # source code or documentation are not subject to the GNU General Public
  18. # License and may only be used or replicated with the express permission of
  19. # Red Hat, Inc.
  20. #
  21. # Red Hat Author(s): Chris Lumens <clumens@redhat.com>
  22. #                    David Cantrell <dcantrell@redhat.com>
  23. #
  24.  
  25. import math
  26. import _ped
  27. import parted
  28. import string
  29.  
  30. from decorators import localeC
  31.  
  32. # XXX: add docstrings
  33.  
  34. class Partition(object):
  35.     @localeC
  36.     def __init__(self, disk=None, type=None, fs=None, geometry=None, PedPartition=None):
  37.         if PedPartition is None:
  38.             if disk is None:
  39.                 raise parted.PartitionException, "no disk specified"
  40.             elif type is None:
  41.                 raise parted.PartitionException, "no type specified"
  42.             elif geometry is None:
  43.                 raise parted.PartitionException, "no geometry specified"
  44.  
  45.             self._fileSystem = fs
  46.             self._geometry = geometry
  47.             self._disk = disk
  48.  
  49.             if fs is None:
  50.                 self.__partition = _ped.Partition(disk.getPedDisk(), type, geometry.start, geometry.end)
  51.             else:
  52.                 self.__partition = _ped.Partition(disk.getPedDisk(), type, geometry.start, geometry.end, parted.fileSystemType[fs.type])
  53.         else:
  54.             self.__partition = PedPartition
  55.             self._geometry = parted.Geometry(PedGeometry=self.__partition.geom)
  56.  
  57.             if disk is None:
  58.                 self._disk = parted.Disk(PedDisk=self.__partition.disk)
  59.             else:
  60.                 self._disk = disk
  61.  
  62.             if self.__partition.fs_type is None:
  63.                 self._fileSystem = None
  64.             else:
  65.                 self._fileSystem = parted.FileSystem(type=self.__partition.fs_type.name, geometry=self._geometry)
  66.  
  67.     def __eq__(self, other):
  68.         return not self.__ne__(other)
  69.  
  70.     def __ne__(self, other):
  71.         if hash(self) == hash(other):
  72.             return False
  73.  
  74.         if type(self) != type(other):
  75.             return True
  76.  
  77.         return self.path != other.path or self.type != other.type or self.geometry != other.geometry or self.fileSystem != other.fileSystem
  78.  
  79.     def __str__(self):
  80.         try:
  81.             name = self.name
  82.         except parted.PartitionException:
  83.             name = None
  84.  
  85.         s = ("parted.Partition instance --\n"
  86.              "  disk: %(disk)r  fileSystem: %(fileSystem)r\n"
  87.              "  number: %(number)s  path: %(path)s  type: %(type)s\n"
  88.              "  name: %(name)s  active: %(active)s  busy: %(busy)s\n"
  89.              "  geometry: %(geometry)r  PedPartition: %(ped)r" %
  90.              {"disk": self.disk, "fileSystem": self.fileSystem, "geometry": self.geometry,
  91.               "number": self.number, "path": self.path,
  92.               "type": self.type, "name": name, "active": self.active,
  93.               "busy": self.busy, "ped": self.__partition})
  94.         return s
  95.  
  96.     def __writeOnly(self, property):
  97.         raise parted.WriteOnlyProperty, property
  98.  
  99.     @property
  100.     @localeC
  101.     def active(self):
  102.         """True if the partition is active, False otherwise."""
  103.         return bool(self.__partition.is_active())
  104.  
  105.     @property
  106.     @localeC
  107.     def busy(self):
  108.         """True if the partition is active, False otherwise."""
  109.         return bool(self.__partition.is_busy())
  110.  
  111.     @property
  112.     def disk(self):
  113.         """The Disk this partition belongs to."""
  114.         return self._disk
  115.  
  116.     @property
  117.     @localeC
  118.     def path(self):
  119.         """The filesystem path to this partition's device node."""
  120.         return self.__partition.get_path()
  121.  
  122.     @property
  123.     @localeC
  124.     def name(self):
  125.         """The name of this partition."""
  126.         try:
  127.             return self.__partition.get_name()
  128.         except parted.PartitionException as msg:
  129.             return None
  130.  
  131.     @property
  132.     def number(self):
  133.         """The partition number."""
  134.         return self.__partition.num
  135.  
  136.     fileSystem = property(lambda s: s._fileSystem, lambda s, v: setattr(s, "_fileSystem", v))
  137.     geometry = property(lambda s: s._geometry, lambda s, v: setattr(s, "_geometry", v))
  138.     system = property(lambda s: s.__writeOnly("system"), lambda s, v: s.__partition.set_system(v))
  139.     type = property(lambda s: s.__partition.type, lambda s, v: setattr(s.__partition, "type", v))
  140.  
  141.     @localeC
  142.     def getFlag(self, flag):
  143.         """Get the value of a particular flag on the partition.  Valid flags
  144.            are the _ped.PARTITION_* constants.  See _ped.flag_get_name() and
  145.            _ped.flag_get_by_name() for more help working with partition flags.
  146.         """
  147.         return self.__partition.get_flag(flag)
  148.  
  149.     @localeC
  150.     def setFlag(self, flag):
  151.         """Set the flag on a partition to the provided value.  On error, a
  152.            PartitionException will be raised.  See getFlag() for more help on
  153.            working with partition flags."""
  154.         return self.__partition.set_flag(flag, 1)
  155.  
  156.     @localeC
  157.     def unsetFlag(self, flag):
  158.         """Unset the flag on this Partition.  On error, a PartitionException
  159.            will be raised.  See getFlag() for more help on working with
  160.            partition flags."""
  161.         return self.__partition.set_flag(flag, 0)
  162.  
  163.     @localeC
  164.     def getMaxGeometry(self, constraint):
  165.         """Given a constraint, return the maximum Geometry that self can be
  166.            grown to.  Raises Partitionexception on error."""
  167.         return parted.Geometry(PedGeometry=self.disk.getPedDisk().get_max_partition_geometry(self.__partition, constraint))
  168.  
  169.     @localeC
  170.     def isFlagAvailable(self, flag):
  171.         """Return True if flag is available on this Partition, False
  172.            otherwise."""
  173.         return self.__partition.is_flag_available(flag)
  174.  
  175.     @localeC
  176.     def nextPartition(self):
  177.         """Return the Partition following this one on the Disk."""
  178.         partition = self.disk.getPedDisk().next_partition(self.__partition)
  179.  
  180.         if partition is None:
  181.             return None
  182.         else:
  183.             return parted.Partition(disk=self.disk, PedPartition=partition)
  184.  
  185.     @localeC
  186.     def getSize(self, unit="MB"):
  187.         """Return the size of the partition in the unit specified.  The unit
  188.            is given as a string corresponding to one of the following
  189.            abbreviations:  b (bytes), KB (kilobytes), MB (megabytes), GB
  190.            (gigabytes), TB (terabytes).  An invalid unit string will raise a
  191.            SyntaxError exception.  The default unit is MB."""
  192.         return self.geometry.getSize(unit)
  193.  
  194.     def getFlagsAsString(self):
  195.         """Return a comma-separated string representing the flags
  196.            on this partition."""
  197.         flags = []
  198.  
  199.         for flag in partitionFlag.keys():
  200.             if self.getFlag(flag):
  201.                 flags.append(partitionFlag[flag])
  202.  
  203.         return string.join(flags, ', ')
  204.  
  205.     def getMaxAvailableSize(self, unit="MB"):
  206.         """Return the maximum size this Partition can grow to by looking
  207.            at contiguous freespace partitions.  The size is returned in
  208.            the unit specified (default is megabytes).  The unit is a
  209.            string corresponding to one of the following abbreviations:
  210.            b (bytes), KB (kilobytes), MB (megabytes), GB (gigabytes),
  211.            TB (terabytes).  An invalid unit string will raise a
  212.            SyntaxError exception."""
  213.         lunit = unit.lower()
  214.  
  215.         if lunit not in parted._exponent.keys():
  216.             raise SyntaxError, "invalid unit %s given" % (unit,)
  217.  
  218.         maxLength = self.geometry.length
  219.         sectorSize = self.geometry.device.sectorSize
  220.  
  221.         for partition in self.disk.partitions:
  222.             if partition.type & parted.PARTITION_FREESPACE:
  223.                 maxLength += partition.geometry.length
  224.             else:
  225.                 break
  226.  
  227.         return math.floor(maxLength * math.pow(sectorSize, parted._exponent[lunit]))
  228.  
  229.     def getDeviceNodeName(self):
  230.         """Return the device name for this Partition."""
  231.         return self.path[5:]
  232.  
  233.     def getPedPartition(self):
  234.         """Return the _ped.Partition object contained in this Partition.
  235.            For internal module use only."""
  236.         return self.__partition
  237.  
  238. # collect all partition flags and store them in a hash
  239. partitionFlag = {}
  240. __flag = _ped.partition_flag_next(0)
  241. partitionFlag[__flag] = _ped.partition_flag_get_name(__flag)
  242. __readFlags = True
  243.  
  244. while __readFlags:
  245.     __flag = _ped.partition_flag_next(__flag)
  246.     if not __flag:
  247.         __readFlags = False
  248.     else:
  249.         partitionFlag[__flag] = _ped.partition_flag_get_name(__flag)
  250.